package com.zz.ecommerce.common;

import org.apache.ibatis.session.RowBounds;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * <公共DAO层>
 * <功能详细描述>
 *
 * @author Administrator
 * @date 2015/9/2，22:30
 */
@Repository
public class CommonBaseDao extends SqlSessionDaoSupport {

    public <T> int commonInsert(String sqlId, T params) {
        return getSqlSession().insert(sqlId, params);
    }

    public <T> int commonDelete(String sqlId, T params) {
        return getSqlSession().delete(sqlId, params);
    }

    public <T> int commonUpdate(String sqlId, T params) {
        return getSqlSession().update(sqlId, params);
    }

    public <T> Object commonObject(String sqlId, T params) {
        return getSqlSession().selectOne(sqlId, params);
    }

    public <T, E> List<T> commonList(String sqlId, E params) {
        return getSqlSession().selectList(sqlId, params);
    }

    public <T, E extends GridRequest> List<T> commonPaginationList(String sqlId, String sqlCountId, E params) {
        ArrayList<T> list = new ArrayList<T>();
        Long rows = (Long) getSqlSession().selectOne(sqlCountId, params);
        params.setTotal(rows);

        if (rows > 0) {
            list = (ArrayList<T>) getSqlSession().selectList(sqlId, params);
        }

        return list;
    }

    public <T, E> List<T> commonPaginationList(String sqlId, E params,RowBounds rowBounds) {
        return getSqlSession().selectList(sqlId, params,rowBounds);
    }
}
